<?php

namespace App\Http\Controllers\admin\user;

use App\Models\Order;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\User;
use App\Models\User_info;
use App\Models\Doctor;
use App\Models\District;
use App\Models\Address;
use App\Models\Order_info;


class AjaxController extends Controller
{
    public function status(Request $request)
    {
        $id = $request->input('id');
        $sta = $request->input('status');
        $user =  User::find($id);
        $user->status = $sta == 1? 2 : 1;
        $bo = $user->save();
        if($bo){
            $sta = $sta == 1 ? 2:1;
            return  json_encode($sta);
        }else{
            return  json_encode($sta);
        }
    }

    public function seladdress(Request $request)
    {
        $id = $request->input('id');
        $address = new Address();
        $bo = $address->addselect($id);
        if($bo){
            return  json_encode($bo);
        }else{
            return  json_encode(null);
        }
    }

    public function regsuc(Request $request)
    {

        $user = new User();
        $user->username = $request->input('username');
        $user->password = md5($request->input('password'));
        $user->save();
        if ($user->id > 0) {
            $user_info = new User_info();
            $user_info->user_id = $user->id;
            $user_info->phone = $request->input('username');
            $user_info->nickname = $request->input('username');
            $info_id =  $user_info->save();
            if($info_id){
                return json_encode('账号注册成功!');
            }else{
                return json_encode("账号存在问题！");
            }
        }else{
            return json_encode("账号存在问题！");
        }
    }

    public function selorder(Request $request)
    {
        $id = $request->input('id');
        $order_info = new Order_info();
        $row = $order_info->listorder($id);
        if($row){
            return  json_encode($row);
        }else{
            return  json_encode(null);
        }
    }

    public function deleteorder(Request $request)
    {
        $id = $request->input('id');
        $order = Order::find($id);
        $order->status = 7;
        $row = $order->save();
        if($row){
            return  json_encode('成功');
        }else{
            return  json_encode('失败');
        }
    }

    public function editorder(Request $request)
    {
        $id = $request->input('id');
        $order = Order::find($id);
        $order->name = $request->input('name') == ''? null : $request->input('name');
        $order->phone = $request->input('phone')== ''? null : $request->input('phone');
        $order->address = $request->input('address')== ''? null : $request->input('address');
        $order->realpay = $request->input('realpay')== ''? null : $request->input('realpay');
        $order->doctor_id = $request->input('doctor_id')== ''? null : $request->input('doctor_id');
        $order->sender_id = $request->input('sender_id')== ''? null : $request->input('sender_id');
        $order->remark = $request->input('remark')== ''? null : $request->input('remark');
        $row = $order->save();
        if($row){
            return  json_encode('成功');
        }else{
            return  json_encode('失败');
        }
    }
}
